<template>
	<view class="container">
		<view class="cu-card">
			<view class="cu-item shadow-warp bank_item" v-for="(item, index) in bankList" :key="index">
				<view class="content" @click="checkBank(item.id)">
					<view class="cu-list menu">
						<view class="cu-item">
							<view class="bank_tit">{{ item.bank }} </view>
							<view style="color:#bfbfbf;" v-if="item.is_default">默认</view>
						</view>
						<view class="cu-item">
							<view class="text-cut bank_account">{{ item.account }}</view>
						</view>	
					</view>
				</view>
			</view>
		</view>
		<view class="tab-bar">
			<view class="info_bg"></view>
			<view class="tab_item" @click="navToUrl('/pages/bank/manage?type=add')">
				<view class="add_img"><image src=""></image></view>
				<view class="add_text">添加银行卡</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bankList:[],
				userinfo:[],
				alipay_name:'',
				alipay_account:'',
				source:0
			};
		},
		onLoad: function (option) {
			this.source = option.source;
			this.helper.checkLogin();
			this.getData();
			this.getBanks();
		},
		onNavigationBarButtonTap(val) {
			uni.navigateTo({
				url: '/pages/bank/manage?type=add',
			});	
		},
		methods: {
			getData:function(){
				//获取基础参数
				// uni.showLoading({
				// 	title:"数据加载中",
				// 	mask:true
				// })
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['username='+info.username,'token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"getUserInfo",
					timestamp:timestamp,
					data:{username:info.username,token:info.token},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					this.userinfo = res.data.data.userinfo;
					this.alipay_name = res.data.data.userinfo.alipay_name;
					this.alipay_account = res.data.data.userinfo.alipay_account;
					uni.hideLoading();
					
					if(this.userinfo.auth == 0){
						uni.showModal({
							title: "提示",
							content: '帐户未实名，现在实名？',
							showCancel:false,
							confirmText: "现在实名",
							success:function(res){
								if(res.confirm){
									uni.navigateTo({
										url: "/pages/user/certification"
									})
								}
							}
						})
					}
				})
			},
			getBanks:function(){
				//获取基础参数
				// uni.showLoading({
				// 	title:"数据加载中",
				// 	mask:true
				// })
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"bankList",
					timestamp:timestamp,
					data:{token:info.token},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					this.bankList = res.data.data.bankList;
					uni.hideLoading();
				})
			},
			delBank:function(bank_id){
				var that = this;
				uni.showModal({
					title: "提示",
					content: "确认删除银行卡？",
					showCancel: true,
					confirmText: "确定",
					success:function(res){
						if(res.confirm){
							uni.showLoading({
								title:"数据删除中",
								mask:true
							})
							var info = uni.getStorageSync("userinfo");
							let timestamp = that.helper.now();
							let sign = that.helper.setSign(['bank_id='+bank_id,'token='+info.token,'timestamp='+timestamp]);
							that.helper.req({
								api:"delBank",
								timestamp:timestamp,
								data:{bank_id:bank_id,token:info.token},
								header:{sign:sign},
								method:'POST',
							}).then(res=>{
								uni.hideLoading();
								if(res.data.errorcode>0){
									uni.showToast({
										title:res.data.message,
										icon:"none"
									})
									return;
								}else{
									uni.showToast({
										title: "银行卡删除成功",
										icon: "success"
									})
									setTimeout(() => {
										uni.redirectTo({
											url: '/pages/bank/index',
										});	
									}, 300);
								}
							})
						}
					}
				})
			},
			checkBank: function(bank_id) {
				if(this.source == 1){
					var pages = getCurrentPages();
					var currPage = pages[pages.length - 1]; //当前页面
					var prevPage = pages[pages.length - 2]; //上一个页面
					//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
					prevPage.$vm.bank_id = bank_id;
					uni.navigateBack();
				}else{
					uni.navigateTo({
						url: '/pages/bank/manage?type=edit&bank_id='+bank_id
					});	
					
				}
			},
			//链接
			navToUrl: function(url) {
				return uni.navigateTo({
					url: url
				});
			},
		},
	};
</script>

<style lang="scss">
	page {
	    min-height: 200vw;
			color: #56595d;
			background-color: #ddeafd;
	}
	.bank_item{
		height: 36vw;
		padding: 5vw 0;
		.cu-item{
			background-color: transparent;
			padding-left: 8vw;
			.bank_tit{
				font-size: 20px;
			}
			.bank_account{
				font-size: 18px;
			}
		}
	}
	.cu-btn{
		height: 50upx;
	}
	.tab-bar {
		width: 92vw;
		height: 40vw;
		margin: 0 4vw;
		position: relative;
		
		.info_bg{
			position: absolute;
			width: 100%;
			height: 100%;
			left: 0;
			top: 0;
			background-color: #fff;
			border-radius:8px;
		}
		
		.tab_item{
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 40vw;
			padding: 10vw 0 0;
			.add_img{
				width: 100%;
				height: 40px;
				text-align: center;
				image{
					width: 8vw;
					height: 8vw;
				}
			}
			.add_text{
				width: 100%;
				line-height: 40px;
				font-size: 18px;
				text-align: center;
			}
		}
	}
</style>
